<template>
	<view>
		<anilSeat :title="movieName" :info="`${movieTime} | ${movieType}`" :room-name="movieRoom" :max="max" :seatData="dataList" @confirm="getInfo">
		</anilSeat>
	</view>
</template>

<script setup>
	import { ref, onMounted } from 'vue';
	import anilSeat from '@/components/anil-seat.vue';
	import { onShow , onLoad } from '@dcloudio/uni-app';
	import { apiSeat } from '@/api/upapi.js';

	// 定义响应式数据
	const dataList = ref([]);
	const max = ref(4);
	const cinemaId = ref('');
	const movieName = ref('');
	const movieRoom = ref('');
	const movieType = ref('');
	const movieTime = ref('');
	const filmId = ref('');
	const showId = ref('');
	const sectionId = ref('');

	
	// 获取数据的函数
	const getData = async(e) => {
		try {
			let res = await apiSeat({ showId: e });
			if(res.data.result==0){
				let arrat = [];
				const processedSeats = res.data.object.seatList.map(seat => {
				    const matches = seat.name.match(/(\d+)排(\d+)座/);
				    return {
				        ...seat, // 保留所有原始字段
				        pai: matches ? parseInt(matches[1]) : 0,  // 排数值
				        lie: matches ? parseInt(matches[2]) : 0  // 列数值
				    };
				});
				
				processedSeats.forEach(item => {
					arrat.push({
						YCoord: item.row,
						XCoord: item.column,
						SeatCode: item.seatId,
						Status: item.status === 1 ? 0 : 1,
						Price: item.showPrice,
						RowNum: item.pai,
						ColumnNum: item.lie,
						seatName: item.name
					});
				});
				setTimeout(() => {
					dataList.value = arrat;
				}, 500);
			}else{
				uni.showToast({ title: res.data.msg, icon:'none' });
			}
		} catch (error) {
			console.error('获取数据失败', error);
		}
	};

	// 处理确认事件的函数
	const getInfo = (e) => {
		console.log('9999===',e);
		let array = [];
		let allPrice = 0;
		let moneys = 0;
		e.forEach(item => {
			array.push({
				seatId: item.SeatCode,
				seatName: `${item.RowNum}排${item.ColumnNum}座`
			});
			allPrice += item.Price;
			moneys = parseFloat(Number(allPrice).toFixed(2));
		});
		console.log('moneys===',moneys);
		let userInfo = {
			showId: showId.value,
			sectionId: sectionId.value,
			showInfor: array,
			filmId:filmId.value,
			money:moneys
		};
		const encodedData = encodeURIComponent(JSON.stringify(userInfo));
		uni.redirectTo({ url: `/pagesA/movie/suozuo?data=${encodedData}` });
	};
	
	onLoad((option) => {
		movieName.value=option.movieName;
		movieRoom.value=option.movieRoom;
		movieType.value=option.movieType;
		movieTime.value=option.movieTime;
		filmId.value=option.filmId;
		showId.value=option.showId;
		sectionId.value=option.sectionId;
		getData(option.showId);
	});
</script>

<style lang="less">

</style>